package com.akcome.apollo.controller;

import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletResponse;

import org.apache.commons.collections4.map.HashedMap;
import org.common.utils.bean.AkResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.PageRequest;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.CookieValue;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.akcome.apollo.common.AkResultHelper;
import com.akcome.apollo.common.ApolloConstants;
import com.akcome.apollo.domain.enums.CoinTypeEnum;
import com.akcome.apollo.dto.PoolLuckyResult;
import com.akcome.apollo.service.BmpAccountService;
import com.akcome.apollo.service.VFoundBlocksService;
import com.akcome.apollo.service.VStatsPoolDayService;
import com.akcome.apollo.service.VStatsPoolHourService;

/**
 * 统计相关
 * @author wn
 *
 */
@Controller
@RequestMapping("/stat")
public class StatController {

	@Autowired
	private VFoundBlocksService vFoundBlocksService;
	
	@Autowired
	private AkResultHelper akResultHelper;
	
	@Autowired
	private VStatsPoolDayService vStatsPoolDayService;
	
	@Autowired
	private BmpAccountService bmpAccountService;
	
	@Autowired
	private VStatsPoolHourService vStatsPoolHourService;
	
	/**
     * 统计
     * @return
     */
    @RequestMapping("/{coinType}")
    public String poolStats(@PathVariable String coinType,@CookieValue(required = false,value=ApolloConstants.COOKIE_ACCOUNT) Long c_accountId, ModelMap modelMap,HttpServletResponse response){
    	coinType = null==CoinTypeEnum.getByCode(coinType)?CoinTypeEnum.BTC.getCode():coinType;
    	modelMap.put("stat_coinType", coinType);
    	bmpAccountService.homeSetting(modelMap, c_accountId, response);
    	return "poolStats";
    }
    
	/**
	 * 查询矿池区块列表
	 * @param page
	 * @param size
	 * @return
	 */
	@ResponseBody
	@RequestMapping("/foundBlocks")
	public AkResult foundBlocks(String coinType,int page,int size){
		return akResultHelper.buildSuccessAkResult(vFoundBlocksService.findByPage(coinType,new PageRequest(page, size)));
	}
	
	/**
	 * 矿池幸运数据
	 * @return
	 * @throws Exception 
	 */
	@ResponseBody
	@RequestMapping("/poolLucky")
	public PoolLuckyResult findPoolLuckyResult(String coinType) throws Exception{
		return vStatsPoolDayService.findPoolLuckyResult(coinType);
	}
	
	/**
	 * 查询矿池算力数据
	 * @param coinType
	 * @return
	 */
	@ResponseBody
	@RequestMapping("/poolData/{coinType}")
	public Map<String, Object> findPoolSpeed(@PathVariable String coinType){
		return vStatsPoolHourService.findIndex(coinType);
	}
	
	@ResponseBody
	@RequestMapping("/poolData/index")
	public Map<String, Object> indexDate(){
		return vStatsPoolHourService.indexDate();
	}
	
}
